//
//  ZAlertView.m
//
//  Created by ZhàoYǒng on 2018/1/22.
//  Copyright © 2018年 ZhàoYǒng. All rights reserved.
//

#import "ZAlertView.h"
#import <Masonry/Masonry.h>

@interface ZAlertView ()

@property (nonatomic, copy) void(^actionBlock)(BOOL cancel);
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *confirmButton;

@end

@implementation ZAlertView

- (instancetype)initWithTitle:(NSString *)title
                  actionTitle:(NSString *)actionTitle
                  cancelTitle:(NSString *)cancelTitle
                  actionBlock:(void(^)(BOOL cancel))actionBlock {
    CGFloat W = UIScreen.mainScreen.bounds.size.width * 0.8f;
    CGFloat marginV = 30.f;
    CGFloat marginH = 20.f;
    CGFloat buttonHeight = 40.f;
    CGFloat buttonWidth = (W - 3 * marginH) / 2;
    CGFloat titleHeight = [title boundingRectWithSize:CGSizeMake(W - marginH * 2, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17.f]} context:nil].size.height;
    CGFloat H = titleHeight + buttonHeight + marginV * 3;
    self = [super initWithFrame:CGRectMake(0, 0, W, H)];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.layer.cornerRadius = 5.f;
        self.actionBlock = actionBlock;
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.font = [UIFont systemFontOfSize:17.f];
        self.titleLabel.textColor = UIColor.blackColor;
        self.titleLabel.text = title;
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(marginV);
            make.left.equalTo(self).offset(marginH);
            make.right.equalTo(self).offset(-marginH);
        }];
        
        if (cancelTitle.length > 0) {
            self.cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
            [self.cancelButton setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
            [self.cancelButton setTitle:cancelTitle forState:UIControlStateNormal];
            self.cancelButton.layer.borderColor = UIColor.lightGrayColor.CGColor;
            self.cancelButton.layer.borderWidth = 1.f;
            self.cancelButton.layer.cornerRadius = buttonHeight / 2;
            self.cancelButton.titleLabel.font = [UIFont systemFontOfSize:15.f];
            [self.cancelButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
            self.cancelButton.tag = 13340;
            [self addSubview:self.cancelButton];
            [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(marginV);
                make.left.equalTo(self).offset(marginH);
                make.height.equalTo(@(buttonHeight));
                make.width.equalTo(@(buttonWidth));
            }];
        }
        
        if (actionTitle.length > 0) {
            self.confirmButton = [UIButton buttonWithType:UIButtonTypeSystem];
            [self.confirmButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            [self.confirmButton setTitle:actionTitle forState:UIControlStateNormal];
            self.confirmButton.backgroundColor = UIColor.blueColor;
            self.confirmButton.layer.cornerRadius = buttonHeight / 2;
            self.confirmButton.titleLabel.font = [UIFont systemFontOfSize:15.f];
            [self.confirmButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
            self.confirmButton.tag = 13341;
            [self addSubview:self.confirmButton];
            if (cancelTitle.length > 0) {
                [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.bottom.width.equalTo(self.cancelButton);
                    make.left.equalTo(self.cancelButton.mas_right).offset(marginH);
                    make.right.equalTo(self).offset(-marginH);
                }];
            } else {
                [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(self.titleLabel.mas_bottom).offset(marginV);
                    make.centerX.equalTo(self);
                    make.height.equalTo(@(buttonHeight));
                    make.width.equalTo(@(buttonWidth));
                }];
            }
        } else if (cancelTitle.length > 0) {
            [self.cancelButton mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.titleLabel.mas_bottom).offset(marginV);
                make.centerX.equalTo(self);
                make.height.equalTo(@(buttonHeight));
                make.width.equalTo(@(buttonWidth));
            }];
        }
    }
    return self;
}

- (void)action:(UIButton *)sender {
    NSInteger index = sender.tag - 13340;
    if (self.actionBlock) self.actionBlock(index == 0);
}

- (void)setTitleColor:(UIColor *)color {
    self.titleLabel.textColor = color;
}

- (void)setCancelTintColor:(UIColor *)color {
    self.cancelButton.backgroundColor = UIColor.clearColor;
    self.cancelButton.layer.borderColor = color.CGColor;
    [self.cancelButton setTitleColor:color forState:UIControlStateNormal];
}

- (void)setConfirmButtonColor:(UIColor *)color {
    self.confirmButton.backgroundColor = color;
}

- (void)setConfirmTitleColor:(UIColor *)color {
    [self.confirmButton setTitleColor:color forState:UIControlStateNormal];
}

- (void)setAlertBackgroundColor:(UIColor *)color {
    self.backgroundColor = color;
}

@end
